
*** Data Descriptives for Product Ban ***


	** Contents
		 
	** a) DESCRIPTIVE STATS (Table 1)
	** b) PRODUCT SPACE BY SEGMENT
	** c) GRAPHS FOR MAIN TEXT
	

capture log close 
clear all
set more off 
*set maxvar 30000, permanently // required to load matrix 

* Set directory
global server "R:\WSV2\TBu_AKe" 
cd "$server" // set working directory 

capture mkdir Bunching_NEW
global store "${server}\Bunching_NEW"

capture mkdir Graphs
global graphs "${store}\Graphs"

*Define sub directories
global desktop "C:\Users\hy65byfe\Desktop\smerge_0712"
*cd "$desktop" // set working directory 

cd "$store"
log using descriptives_1502, replace 

cd // display in log file 	

		
		** a) 
		**** Descriptives: data at bin level ****
		
use spatial_4_sample_s, replace 

tabstat units eei kwh capacity if year == 2011 & eei_bin >= 59, stats(mean sd min max n) col(stat) long f(%9.3f) // R, 2011

tabstat units eei kwh capacity if year == 2011 & eei_bin < 59, stats(mean sd min max n) col(stat) long f(%9.3f) // U, 2011

tabstat units eei kwh capacity if year == 2014 & eei_bin < 59, stats(mean sd min max n) col(stat) long f(%9.3f) // U, 2014

		** b) 
		**** Bunching Segment ****
		
tabstat units eei kwh capacity if year == 2011 & eei_bin > 57 & eei_bin < 59, stats(mean sd min max n) col(stat) long f(%9.3f) // B, 2011

tabstat units eei kwh capacity if year == 2014 & eei_bin > 57 & eei_bin < 59, stats(mean sd min max n) col(stat) long f(%9.3f) // B, 2014


		*** Disappearing cells ***
codebook cell if year == 2011 & eei_bin > 59 & units > 1 // 686 

codebook cell if year == 2014 & eei_bin > 59 & units > 1 // 400 --> 286 disappeared 

codebook cell if year == 2011 & eei_bin < 59 & eei_bin > 57 & units > 1 // 686 

codebook cell if year == 2014 & eei_bin < 59 & eei_bin > 57 & units > 1 // 400 --> 286 disappeared 

twoway kdensity units if year == 2014 & eei_bin > 59 & units < 1000 || kdensity units if year == 2011 & eei_bin > 59 & units < 1000 // see spike at zero and irregularity due to missing bins in 2014 curve


		*** Clearance inventory ***
		
		* Cell-level product count 
gen restricted = 0 		
replace restricted = 1 if eei_bin > 59 & eei_bin != . 

by restricted year, 	sort: egen sr=sum(units)

by year, 	sort: egen st=sum(units)

gen rr = sr/st if restricted == 1

tab rr if year == 2014

*log close 

clear 

		** c) Graphs in paper 

cd "$store"

capture mkdir Graphs
global graphs "${store}\Graphs"

	** load data ** 
use "R:\WSV2\TBu_AKe\Product Ban\Stata\Bunching_Empirics\WM_V10_precollapse_binbasis.dta"
 
gen kwh = ae // avoid confusion with E,e : yearly consumption as defined by regulation.  

 ** basic descriptives
summ kwh capacity, detail 
drop if kwh > 600 // after manual check against product menu. max: DAEWOO can be dropped, reduce to 600. 
sum kwh capacity, detail // main attributes
sum eei, detail // starts at 13.5 

gen units_pos = units
gen unit_weight = round(units_pos, 1)
replace unit_weight = 0 if units < 0 

**------- Figure 1: Bunch_Ban -------- **
#delimit;
graph twoway (scatter ae capacity if ae < 1000 & eei_bin < 58, msymbol(smcircle_hollow) mcolor(dkgreen)) 
			 (scatter ae capacity if ae < 1000 & eei_bin > 58, msymbol(smdiamond_hollow) mcolor(grey)) 
			 (scatter ae capacity if ae < 1000 & inrange(eei_bin,57.5,58.5), msymbol(smdiamond_hollow) mcolor(red)), 
			 legend(label(1 "EEI < 58") label(2 "EEI ≥ 59 ") label(3 "57.5 ≤ EEI < 59") row(1)) graphregion(color(white)) plotregion(lcolor(black))
			 title("", color(black) size(medsmall)) xtitle("Capacity") ytitle("Energy Consumption")
			 ylabel(0(100)600, labsize(small)) xlabel(2(2)16, labsize(small)) xscale(range(2 16)); #delimit cr

cd "$graphs" 
graph save 	 FigD1_banned.gph, replace
graph export FigD1_banned.eps, as(eps) fontface("Baskerville Old Face") replace
graph export FigD1_banned.png, as(png) width(1500) replace


**------- Figure 2: Capacity -------- **

#delimit;
histogram capacity [fw=unit_weight] if year == 2011 & inrange(capacity,3.5,10) & inrange(capacity,3.5,10) & eei_bin < 100 & eei_bin > 50
, fraction fcolor(ebg) lcolor(black%70) lwidth(vthin) start(3.5) width(0.1)
xtitle("Capacity (kg)") ytitle("Percent of Sales") graphregion(color(white)) plotregion(lcolor(black))
			 title("", color(black) size(medsmall)) 
			 ylabel(0(0.1)0.4, labsize(small)) xlabel(3(1)10, labsize(small)) yscale(range(0 0.4))
; #delimit cr

graph save 	 FigD2_a11_trunc.gph, replace
graph export FigD2_a11_trunc.eps, as(eps) fontface("Baskerville Old Face") replace
graph export FigD2_a11_trunc.png, as(png) width(1500) replace


#delimit;
histogram capacity [fw=unit_weight] if year == 2014 & inrange(capacity,3.5,10)  & eei_bin < 100 & eei_bin > 50
, fraction fcolor(dkgreen) lcolor(black%70) lwidth(vthin) start(3.5) width(0.1)
xtitle("Capacity (kg)") ytitle("Percent of Sales") graphregion(color(white)) plotregion(lcolor(black))
			 title("", color(black) size(medsmall)) 
			 ylabel(0(0.1)0.4, labsize(small)) xlabel(3(1)10, labsize(small)) yscale(range(0 0.4))
; #delimit cr

graph save 	 FigD2_a14_trunc.gph, replace
graph export FigD2_a14_trunc.eps, as(eps) fontface("Baskerville Old Face") replace
graph export FigD2_a14_trunc.png, as(png) width(1500) replace



**------- Figure 3: Energy Consumption -------- **

#delimit;
histogram kwh [fw=unit_weight] if year == 2011 & inrange(kwh,122,299) & eei_bin < 100 & eei_bin > 50
, fraction fcolor(ebg) lcolor(black%70) lwidth(vthin) start(122) width(3)
xtitle("Energy Consumption (kWh/year)") ytitle("Percent of Sales") graphregion(color(white)) plotregion(lcolor(black))
			 title("", color(black) size(medsmall)) 
			 ylabel(0(0.05)0.2, labsize(small)) xlabel(100(50)300, labsize(small)) yscale(range(0 0.2))
; #delimit cr

graph save 	 FigD2_e11_trunc.gph, replace
graph export FigD2_e11_trunc.eps, as(eps) fontface("Baskerville Old Face") replace
graph export FigD2_e11_trunc.png, as(png) width(1500) replace


#delimit;
histogram kwh [fw=unit_weight] if year == 2014 & inrange(kwh,122,299) & eei_bin < 100 & eei_bin > 50
, fraction fcolor(dkgreen) lcolor(black%70) lwidth(vthin) start(120) width(3)
xtitle("Energy Cpnsumption (kWh/year)") ytitle("Percent of Sales") graphregion(color(white)) plotregion(lcolor(black))
			 title("", color(black) size(medsmall)) 
			 ylabel(0(0.05)0.2, labsize(small)) xlabel(100(50)300, labsize(small)) yscale(range(0 0.2))
; #delimit cr

graph save 	 FigD2_e14_trunc.gph, replace
graph export FigD2_e14_trunc.eps, as(eps) fontface("Baskerville Old Face") replace
graph export FigD2_e14_trunc.png, as(png) width(1500) replace



**------- Unweighted: Capacity -------- **

#delimit;
histogram capacity if year == 2011 & inrange(capacity,3.5,10) & inrange(capacity,3.5,10) & eei_bin < 100 & eei_bin > 50
, fraction fcolor(ebg) lcolor(black%70) lwidth(vthin) start(3.5) width(0.1)
xtitle("Capacity (kg)") ytitle("Percent of Sales") graphregion(color(white)) plotregion(lcolor(black))
			 title("", color(black) size(medsmall)) 
			 ylabel(0(0.1)0.4, labsize(small)) xlabel(3(1)10, labsize(small)) yscale(range(0 0.4))
; #delimit cr

graph save 	 FigD2_a11_nw.gph, replace
graph export FigD2_a11_nw.eps, as(eps) fontface("Baskerville Old Face") replace
graph export FigD2_a11_nw.png, as(png) width(1500) replace


#delimit;
histogram capacity if year == 2014 & inrange(capacity,3.5,10)  & eei_bin < 100 & eei_bin > 50
, fraction fcolor(dkgreen) lcolor(black%70) lwidth(vthin) start(3.5) width(0.1)
xtitle("Capacity (kg)") ytitle("Percent of Sales") graphregion(color(white)) plotregion(lcolor(black))
			 title("", color(black) size(medsmall)) 
			 ylabel(0(0.1)0.4, labsize(small)) xlabel(3(1)10, labsize(small)) yscale(range(0 0.4))
; #delimit cr

graph save 	 FigD2_a14_nw.gph, replace
graph export FigD2_a14_nw.eps, as(eps) fontface("Baskerville Old Face") replace
graph export FigD2_a14_nw.png, as(png) width(1500) replace



**------- Unweighted: Energy Consumption -------- **

#delimit;
histogram kwh if year == 2011 & inrange(kwh,122,299) & eei_bin < 100 & eei_bin > 50
, fraction fcolor(ebg) lcolor(black%70) lwidth(vthin) start(122) width(3)
xtitle("Energy Consumption (kWh/year)") ytitle("Percent of Sales") graphregion(color(white)) plotregion(lcolor(black))
			 title("", color(black) size(medsmall)) 
			 ylabel(0(0.05)0.2, labsize(small)) xlabel(100(50)300, labsize(small)) yscale(range(0 0.2))
; #delimit cr

graph save 	 FigD2_e11_nw.gph, replace
graph export FigD2_e11_nw.eps, as(eps) fontface("Baskerville Old Face") replace
graph export FigD2_e11_nw.png, as(png) width(1500) replace


#delimit;
histogram kwh if year == 2014 & inrange(kwh,122,299) & eei_bin < 100 & eei_bin > 50
, fraction fcolor(dkgreen) lcolor(black%70) lwidth(vthin) start(120) width(3)
xtitle("Energy Cpnsumption (kWh/year)") ytitle("Percent of Sales") graphregion(color(white)) plotregion(lcolor(black))
			 title("", color(black) size(medsmall)) 
			 ylabel(0(0.05)0.2, labsize(small)) xlabel(100(50)300, labsize(small)) yscale(range(0 0.2))
; #delimit cr

graph save 	 FigD2_e14_nw.gph, replace
graph export FigD2_e14_nw.eps, as(eps) fontface("Baskerville Old Face") replace
graph export FigD2_e14_nw.png, as(png) width(1500) replace


preserve
sort year eei_bin country month
#delimit ;
collapse (firstnm) id_cn capacity ae ae2 eei eei2 energy_label_eu energy_efficiency class_A wm_labelbin revs_min_2 type_wm_2 bunch* banned*
		 (max) tot_all* sum_countries sum_units* newid exitid (min) product_count* units_bin units_country_bin*
		 (sum) n_all* flag3 flag4 units_clean units
		 (mean) price price_all price_eur lifespan* age_country age_xc age_country_reverse, 
		 by(eei_bin country year) ;
#delimit cr	
gen logunits = ln(units)
rename ae ae_raw
rename ae2 ae 
rename eei eei_raw
rename eei2 eei  
	

replace banned = 0 if eei_bin < 59
replace banned = 1 if eei_bin >= 59

gen units_pos = units

gen unit_weight = round(units_pos, 1)
replace unit_weight = 0 if eei_bin >= 59 & year > 2013 // exclude rest inventory 

replace product_count_bin = 0 if eei_bin >= 59 & year > 2013


*******-----------------------------------------------***
* Create bunching graph *
*******-----------------------------------------------***
cd "$graphs"

	*** Sales-weighted ***
	
		** 2011 ** 
#delimit;
graph twoway histogram eei_bin if year == 2011 & eei_bin < 68 & eei_bin > 53 [fw=unit_weight], 
start(53.5)width(0.4) fraction xline(58.7) fcolor(ebg) lcolor(black%70) lwidth(vthin) xtitle("") 
ytitle("Percent of Sales") legend(off) graphregion(color(white)) plotregion(lcolor(black))
ylabel(0(0.1)0.4, labsize(small)) xlabel(54(2)68, labsize(small)) yscale(range(0 0.4))
;
#delimit cr	

graph save 	 FigR1_histo_sw_localbin11.gph, replace
graph export FigR1_histo_sw_localbin11.eps, as(eps) fontface("Baskerville Old Face") replace
graph export FigR1_histo_sw_localbin11.png, as(png) width(1500) replace


		** 2014 **

#delimit;
graph twoway histogram eei_bin if year == 2014 & eei_bin < 68 & eei_bin > 53 [fw=unit_weight], 
start(53.5)width(0.4) fraction xline(58.7) fcolor(dkgreen) lcolor(black%70) lwidth(vthin) xtitle("") 
ytitle("Percent of Sales") legend(off) graphregion(color(white)) plotregion(lcolor(black))
ylabel(0(0.1)0.4, labsize(small)) xlabel(54(2)68, labsize(small)) yscale(range(0 0.4))
;
#delimit cr	

graph save 	 FigR1_histo_sw_localbin14.gph, replace
graph export FigR1_histo_sw_localbin14.eps, as(eps) fontface("Baskerville Old Face") replace
graph export FigR1_histo_sw_localbin14.png, as(png) width(1500) replace


	*** Unweighted ***
	
		** 2011 ** 
#delimit;
graph twoway histogram eei_bin if year == 2011 & eei_bin < 68 & eei_bin > 53 [fw=product_count_bin], 
start(53.5)width(0.4) fraction xline(58.7) fcolor(ebg) lcolor(black%70) lwidth(vthin) xtitle("") 
ytitle("Percent of Sales") legend(off) graphregion(color(white)) plotregion(lcolor(black))
ylabel(0(0.1)0.4, labsize(small)) xlabel(54(2)68, labsize(small)) yscale(range(0 0.4))
;
#delimit cr	

graph save 	 FigR1_histo_nw_localbin11.gph, replace
graph export FigR1_histo_nw_localbin11.eps, as(eps) fontface("Baskerville Old Face") replace
graph export FigR1_histo_nw_localbin11.png, as(png) width(1500) replace


		** 2014 **

#delimit;
graph twoway histogram eei_bin if year == 2014 & eei_bin < 68 & eei_bin > 53 [fw=product_count_bin], 
start(53.5)width(0.4) fraction xline(58.7) fcolor(dkgreen) lcolor(black%70) lwidth(vthin) xtitle("") 
ytitle("Percent of Sales") legend(off) graphregion(color(white)) plotregion(lcolor(black))
ylabel(0(0.1)0.4, labsize(small)) xlabel(54(2)68, labsize(small)) yscale(range(0 0.4))
;
#delimit cr	

graph save 	 FigR1_histo_nw_localbin14.gph, replace
graph export FigR1_histo_nw_localbin14.eps, as(eps) fontface("Baskerville Old Face") replace
graph export FigR1_histo_nw_localbin14.png, as(png) width(1500) replace


log close 

clear 

exit


